package isp.apps.example.shoppingadmin.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.action.LoadOrderStatusAction;
import isp.apps.example.shoppingcart.domain.ShoppingCartValidator;
import isp.apps.example.shoppingcart.domain.Item;
import isp.apps.example.shoppingcart.domain.OrderStatus;
import isp.apps.example.shoppingcart.persistence.ItemRepository;
import isp.apps.example.shoppingcart.persistence.OrderStatusRepository;
import isp.apps.example.shoppingcart.persistence.factory.ItemPersistenceFactory;
import isp.apps.example.shoppingcart.persistence.factory.OrderStatusPersistenceFactory;

import java.math.BigDecimal;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class OrderStatusAdminAction extends ActionSupport
{
  private Map orderStatusValidMap;
  private Map orderStatusStepMap;
  private Map orderStatusNameMap;
  
  private List allOrderStatuses;
  private List validOrderStatuses;
  private List minValidStatus;
  private List maxValidStatus;
  
  private OrderStatusRepository osr = OrderStatusPersistenceFactory.getOrderStatusRepository();
  
  private String newOrderStatusName, newOrderStatusStep, newOrderStatusValid;
  
  public OrderStatusAdminAction()
  {
  }
  
  public String execute()
  {
    return SUCCESS;
  }
  

  public String updateOrderStatus()
  {
  
    LoadOrderStatusAction load = new LoadOrderStatusAction();
    load.execute();
    allOrderStatuses = load.getAllOrderStatuses();
    
    validateOrderStatusUpdates();
    
    if (!hasActionErrors())
      writeToRepository();
    
    newOrderStatusName = null;
    newOrderStatusStep = null;
    newOrderStatusValid = null;
    
    return SUCCESS;
    
  }

  
  public String newOrderStatus()
  {
    OrderStatus newOS = new OrderStatus();
    
    validateNewOrderStatus();
    
    if (hasActionErrors())
      return SUCCESS;

    newOS.setName(newOrderStatusName);
    newOS.setStep(new Integer(newOrderStatusStep));
    newOS.setValid(new Integer(1));
    

    osr.saveOrderStatus(newOS);
    return SUCCESS;
  }


  public Map getOrderStatusValidMap()
  {
    if (orderStatusValidMap == null)
      orderStatusValidMap = new HashMap();
    return orderStatusValidMap;
  }

  public Map getOrderStatusStepMap()
  {
    if (orderStatusStepMap == null)
       orderStatusStepMap = new HashMap();
    return orderStatusStepMap;
  }

  public Map getOrderStatusNameMap()
  {
    if (orderStatusNameMap == null)
       orderStatusNameMap = new HashMap();
    return orderStatusNameMap;
  }

   public void setMinValidStatus(List minValidStatus)
  {
    this.minValidStatus = minValidStatus;
  }

  public void setMaxValidStatus(List maxValidStatus)
  {
    this.maxValidStatus = maxValidStatus;
  }

  public void setAllOrderStatuses(List allOrderStatuses)
  {
    this.allOrderStatuses = allOrderStatuses;
  }

  public void setValidOrderStatuses(List validOrderStatuses)
  {
    this.validOrderStatuses = validOrderStatuses;
  }

  public void setNewOrderStatusName(String newOrderStatusName)
  {
    this.newOrderStatusName = newOrderStatusName;
  }

  public String getNewOrderStatusName()
  {
    return newOrderStatusName;
  }

  public void setNewOrderStatusStep(String newOrderStatusStep)
  {
    this.newOrderStatusStep = newOrderStatusStep;
  }

  public String getNewOrderStatusStep()
  {
    return newOrderStatusStep;
  }

  public void setOrderStatusValidMap(Map orderStatusValidMap)
  {
    this.orderStatusValidMap = orderStatusValidMap;
  }

  public void setOrderStatusStepMap(Map orderStatusStepMap)
  {
    this.orderStatusStepMap = orderStatusStepMap;
  }

  public void setOrderStatusNameMap(Map orderStatusNameMap)
  {
    this.orderStatusNameMap = orderStatusNameMap;
  }
  
  
  public void validateNewOrderStatus()
  {
    ShoppingCartValidator scv = new ShoppingCartValidator();
    
    if ((newOrderStatusStep = scv.checkNumber(newOrderStatusStep)) == null)
    {
      addActionError("Please specify a valid step number (Integer >= 0). \"" +newOrderStatusStep+ "\"");
      return;
    }
    
    if ((newOrderStatusName = scv.checkText(newOrderStatusName)) == null)
    {
      addActionError("Please specify a valid step name. \"" +newOrderStatusName+ "\"");
      return;
    }
  }


  public void validateOrderStatusUpdates()
  {
    
    
    OrderStatus os = new OrderStatus();
    Integer id = 0;
    ShoppingCartValidator scv = new ShoppingCartValidator();
    
    for (Iterator i = allOrderStatuses.iterator() ; i.hasNext();)
    {
      os = (OrderStatus)i.next();
      id = os.getId();
      newOrderStatusName = (String)orderStatusNameMap.get(id.toString());
      newOrderStatusStep = (String)orderStatusStepMap.get(id.toString());
      newOrderStatusValid = (String)orderStatusValidMap.get(id.toString());
      
      //only validates name and step NOT the valid field
      validateNewOrderStatus();
      
      if ((newOrderStatusValid = scv.checkBoolean(newOrderStatusValid)) == null)
      {
        addActionError("Please specify a '1' or a '0' for the step valid field. \"" +newOrderStatusValid+ "\"");
        return;
      }
    }
    
  }



  public void writeToRepository()
  {
  
    //ItemRepository ir = ItemPersistenceFactory.getItemRepository();
    //osr;
    OrderStatus os = new OrderStatus();
    Integer id = 0;
    for (Iterator i = allOrderStatuses.iterator(); i.hasNext();)
    {
      //orderStatusNameMap orderStatusStepMap orderStatusValidMap
    
      os = (OrderStatus)i.next();
      id = os.getId();
      os.setName((String)orderStatusNameMap.get(id.toString()));
      os.setStep(new Integer((String)orderStatusStepMap.get(id.toString())));
      os.setValid(new Integer((String)orderStatusValidMap.get(id.toString())));
      osr.saveOrderStatus(os);
    }
  }



}
